package com.oriental.manage.service.specially.Impl;

import com.oriental.check.commons.util.BeanMapperUtil;
import com.oriental.manage.dao.specially.PersonRiskInfoReportMapper;
import com.oriental.manage.model.specially.AreaNumber;
import com.oriental.manage.pojo.specially.BatchMessage;
import com.oriental.manage.pojo.specially.PersonRiskInfo;
import com.oriental.manage.service.specially.BatchMessageProcess;
import com.oriental.riskfront.api.enums.MessageStatus;
import com.oriental.riskfront.api.model.specially.PersonRiskInfoUpdateDto;
import com.oriental.riskfront.api.service.DataDictInterface;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by luoxin on 2017/4/25.
 */
@Service
@Slf4j
public class PersonRiskInfoUpdateProcessImpl implements BatchMessageProcess<PersonRiskInfo,PersonRiskInfoUpdateDto>{

    @Autowired
    private PersonRiskInfoReportMapper  personRiskInfoReportMapper;
    @Autowired
    private DataDictInterface dataDictInterface;
    @Override
    public PersonRiskInfo queryDetail(BatchMessage batchMessage) {
        PersonRiskInfo personRiskInfo=personRiskInfoReportMapper.queryById(batchMessage.getSubmitMsgId());
        List<Map<String, Object>> dataAddress = dataDictInterface.getDataAddress("ADDRESS_Select");
        {
            String areaCode=personRiskInfo.getOccurarea();
            //String areaCode="210000,220000,210100";
            if(areaCode!=null && !"".equals(areaCode)) {
                String[] stringArr = areaCode.split(",");
                List<AreaNumber> basicData = new ArrayList<>();
                for (int i = 0; i < stringArr.length; i++) {
                    AreaNumber areaNumber = new AreaNumber();
                    for (Map<String, Object> map : dataAddress) {
                        String areaCo = (String) map.get("areaCode");
                        String areaCoSub = areaCo.substring(0, 2);
                        String areaNa = (String) map.get("areaName");
                        if (areaCo.equals(stringArr[i])) {
                            areaNumber.setAreaCode(areaCo);
                            areaNumber.setAreaName(areaNa);
                            areaNumber.setCityName(areaNa);
                            basicData.add(areaNumber);
                        } else {
                            if (stringArr[i].length() > 1) {
                                if (areaCoSub.equals(stringArr[i].substring(0, 2))) {
                                    areaNumber.setAreaCode(areaCo);
                                    areaNumber.setAreaName(areaNa);
                                    List<Map<String, Object>> citys = (List<Map<String, Object>>) map.get("citys");
                                    for (Map<String, Object> mapCity : citys) {
                                        String cityCo = (String) mapCity.get("cityCode");
                                        String cityNa = (String) mapCity.get("cityName");
                                        if (cityCo.equals(stringArr[i])) {
                                            areaNumber.setCityCode(cityCo);
                                            areaNumber.setCityName(areaNa+cityNa);
                                        }
                                    }
                                    basicData.add(areaNumber);
                                }
                            }
                        }
                    }
                }
                personRiskInfo.setBasicData(basicData);
            }
        }
        return personRiskInfo;
    }

    @Override
    public List query(List<BatchMessage> list) {
        List<PersonRiskInfo> List= personRiskInfoReportMapper.queryByListForUpdate(list);
        List<PersonRiskInfoUpdateDto> bodyList= new ArrayList<>();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat formatter2 = new SimpleDateFormat("yyyy-MM-dd");
        for (PersonRiskInfo personRiskInfo : List) {
          //  personRiskInfo.setValidDate(DateUtil.parse(personRiskInfo.getValidDate(),DateUtil.shotPattern));
           // personRiskInfo.setRepDate(DateUtil.format(personRiskInfo.getRepDate(),DateUtil.datefullPattern));

            String dateString = formatter.format(personRiskInfo.getRepDate());
            PersonRiskInfoUpdateDto body= BeanMapperUtil.objConvert(personRiskInfo,PersonRiskInfoUpdateDto.class);
            body.setRepDate(dateString.toString());
            body.setValidDate(formatter2.format(personRiskInfo.getValidDate()).toString());
            body.setOccurtimeb(personRiskInfo.getOccurtimeb()==null?"":formatter2.format(personRiskInfo.getOccurtimeb()));
            body.setOccurtimee(personRiskInfo.getOccurtimee()==null?"":formatter2.format(personRiskInfo.getOccurtimee()));
            body.setId(personRiskInfo.getRiskId());
            bodyList.add(body);
        }
        log.info("个人风险信息变更报文,bodyList:{}",bodyList);
        return bodyList;
    }


    @Override
    @Transactional
    public void auditFail(List<BatchMessage> batchMessage) {
        for (BatchMessage messageBatchPo : batchMessage) {
            //审核失败
            personRiskInfoReportMapper.updateStatus(messageBatchPo.getSubmitMsgId(), MessageStatus.REJECT.getCode(),messageBatchPo.getContent());
        }
    }
    @Override
    @Transactional
    public void callback(String status, List<BatchMessage> batchMessage) {
        for (BatchMessage messageBatchPo : batchMessage) {

            personRiskInfoReportMapper.updateStatus(messageBatchPo.getSubmitMsgId(),status,messageBatchPo.getContent());
        }
    }
}
